package cn.jinjimi.app.main.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.modules.Resource.NetResourceManager;

import java.util.Map;

/**
 * Created by Roy
 * Date: 2017/3/10
 */

public class MyWebView extends WebView {
    boolean isDestory;

    public MyWebView(Context context) {
        super(context);
    }

    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void destroy() {
        this.isDestory = true;
        super.destroy();
    }

    public boolean isDestory() {
        return isDestory;
    }

    @Override
    public void loadUrl(String url) {
        onPageStart(url);
        super.loadUrl(url);
    }

    private void onPageStart(String url) {
        if (StringUtils.isNotEmpty(url) && url.startsWith("http")) {
            NetResourceManager.getInstance().onPageStart(url);
        }
    }

    @Override
    public void loadUrl(String url, Map<String, String> additionalHttpHeaders) {
        onPageStart(url);
        super.loadUrl(url, additionalHttpHeaders);
    }
}
